﻿namespace ReplayDataFile
{
    using System;
    using System.Collections.Generic;

    public class RecordZipper
    {
        private static byte[] bitMask = new byte[] { 1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80 };
        private Dictionary<int, DataRecord> lastFullRecordDict = new Dictionary<int, DataRecord>();

        public DataRecord Zip(DataRecord rec, bool isRelateZip)
        {
            DataRecord record = null;
            if (this.lastFullRecordDict.ContainsKey(rec.DataType))
            {
                record = this.lastFullRecordDict[rec.DataType];
            }
            this.lastFullRecordDict[rec.DataType] = rec;
            if (isRelateZip)
            {
                int num2;
                if (record == null)
                {
                    return rec;
                }
                if (rec.RecType != DataRecordType.FullDataRecord)
                {
                    return rec;
                }
                if (record.RecType != DataRecordType.FullDataRecord)
                {
                    return rec;
                }
                if (record.DataBuffer.Length != rec.DataBuffer.Length)
                {
                    return rec;
                }
                byte[] dataBuffer = record.DataBuffer;
                byte[] buffer2 = rec.DataBuffer;
                int num = 0;
                for (num2 = 0; num2 < dataBuffer.Length; num2++)
                {
                    if (dataBuffer[num2] == buffer2[num2])
                    {
                        num++;
                    }
                }
                byte num3 = 0;
                byte[] buffer3 = null;
                if (num == dataBuffer.Length)
                {
                    buffer3 = new byte[0];
                    num3 = 1;
                }
                else
                {
                    int num6;
                    int num7;
                    int num4 = (dataBuffer.Length + 7) / 8;
                    int num5 = (num4 + dataBuffer.Length) - num;
                    if (dataBuffer.Length < 0xffff)
                    {
                        num6 = (dataBuffer.Length - num) * 3;
                        if (num6 < num5)
                        {
                            num3 = 2;
                        }
                    }
                    if ((num3 == 0) && (num5 < dataBuffer.Length))
                    {
                        num3 = 3;
                    }
                    if (num3 == 2)
                    {
                        num6 = (dataBuffer.Length - num) * 3;
                        buffer3 = new byte[num6];
                        num7 = 0;
                        for (num2 = 0; num2 < dataBuffer.Length; num2++)
                        {
                            if (dataBuffer[num2] != buffer2[num2])
                            {
                                buffer3[num7] = (byte) (num2 & 0xff);
                                buffer3[num7 + 1] = (byte) (num2 >> 8);
                                buffer3[num7 + 2] = buffer2[num2];
                                num7 += 3;
                            }
                        }
                    }
                    else if (num3 == 3)
                    {
                        byte[] buffer4 = new byte[num5];
                        buffer3 = buffer4;
                        num7 = 0;
                        for (num2 = 0; num2 < dataBuffer.Length; num2++)
                        {
                            if (dataBuffer[num2] != buffer2[num2])
                            {
                                buffer4[num4 + num7] = buffer2[num2];
                                num7++;
                                int index = num2 / 8;
                                int num9 = num2 % 8;
                                buffer4[index] = (byte) (buffer4[index] | bitMask[num9]);
                            }
                        }
                    }
                }
                if (num3 != 0)
                {
                    return new DataRecord { RecType = rec.RecType, DataType = rec.DataType, DataVersion = rec.DataVersion, RecordNo = rec.RecordNo, Time = rec.Time, ZipType = num3, DataBuffer = buffer3 };
                }
            }
            return rec;
        }
    }
}

